import { defineStore } from "pinia";
import { ref } from "vue";
import axios from "axios";
import { ElMessage } from "element-plus";

const useRentManagementStore = defineStore("useRentManagementStore", () => {
    const tableData = ref([]);
    const baseURL = `http://localhost:8080`;
    let url = '';

    // 租赁信息查询的方法
    const Search = async () => {
        url = `${baseURL}/rentmanagements/getList`;

        try {
            const response = await axios.get(url);
            if (response.data && response.data.code === 1) {
                tableData.value = response.data.data || [];
                console.log('查询成功:', response.data.message);
            } else {
                console.error('查询失败:', response.data.message);
                ElMessage({
                    type: 'error',
                    message: '查询失败，请检查输入条件或稍后再试。',
                });
            }
        } catch (error) {
            console.error('请求出错:', error);
            ElMessage({
                type: 'error',
                message: '服务器请求失败，请检查网络连接或稍后再试。',
            });
        }
    };

    // 查询单个租赁信息的方法
    const SearchOne = async (rentId) => {
        url = `${baseURL}/rentmanagements/get/${rentId}`;

        try {
            const response = await axios.get(url);
            if (response.data && response.data.code === 1) {
                tableData.value = [response.data.data] || [];
                console.log('查询成功:', response.data.message);
            } else {
                console.error('查询失败:', response.data.message);
                ElMessage({
                    type: 'error',
                    message: '查询失败，请检查输入条件或稍后再试。',
                });
            }
        } catch (error) {
            console.error('请求出错:', error);
            ElMessage({
                type: 'error',
                message: '服务器请求失败，请检查网络连接或稍后再试。',
            });
        }
    };

    // 删除租赁信息的方法
    const DeleteRent = async (rentId) => {
        url = `${baseURL}/rentmanagements/delete/${rentId}`;
        try {
            const response = await axios.delete(url);
            if (response.data && response.data.code === 1) {
                tableData.value = tableData.value.filter(rent => rent.rentId !== rentId);
                console.log('删除成功:', response.data.message);
                ElMessage({
                    type: 'success',
                    message: '删除成功',
                });
            } else {
                console.error('删除失败:', response.data.message);
                ElMessage({
                    type: 'error',
                    message: '删除失败，请稍后再试。',
                });
            }
        } catch (error) {
            console.error('请求出错:', error);
            ElMessage({
                type: 'error',
                message: '服务器请求失败，请检查网络连接或稍后再试。',
            });
        }
    };

    // 更新租赁信息的方法
    const UpdateRent = async (rent) => {
        url = `${baseURL}/rentmanagements/update`;
        try {
            const response = await axios.put(url, rent);
            if (response.data && response.data.code === 1) {
                const index = tableData.value.findIndex(item => item.rentId === rent.rentId);
                if (index !== -1) {
                    tableData.value[index] = rent;
                }
                console.log('更改成功:', response.data.message);
                ElMessage({
                    type: 'success',
                    message: '更改成功',
                });
            } else {
                console.error('更改失败:', response.data.message);
                ElMessage({
                    type: 'error',
                    message: '更改失败，请稍后再试。',
                });
            }
        } catch (error) {
            console.error('请求出错:', error);
            ElMessage({
                type: 'error',
                message: '服务器请求失败，请检查网络连接或稍后再试。',
            });
        }
    };

    // 新增租赁信息的方法
    const AddRent = async (rent) => {
        url = `${baseURL}/rentmanagements/addDate`;
        try {
            const response = await axios.post(url, rent);
            if (response.data && response.data.code === 1) {
                tableData.value.push(rent);
                console.log('新增成功:', response.data.message);
                ElMessage({
                    type: 'success',
                    message: '新增成功',
                });
            } else {
                console.error('新增失败:', response.data.message);
                ElMessage({
                    type: 'error',
                    message: '新增失败，请稍后再试。',
                });
            }
        } catch (error) {
            console.error('请求出错:', error);
            ElMessage({
                type: 'error',
                message: '服务器请求失败，请检查网络连接或稍后再试。',
            });
        }
    };

    return {
        tableData,
        Search,
        SearchOne,
        DeleteRent,
        UpdateRent,
        AddRent
    }
});

export default useRentManagementStore;
